<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <style>
        body, html {
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
        }

        #allmap {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }

        .result {
            position: fixed;
            top: 10px;
            left: 10px;
            padding: 0px 7px;
            min-width: 350px;
            height: 70px;
            line-height: 35px;
            background: #fff;
            box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
            border-radius: 7px;
            z-index: 99;
        }
    </style>
    <script src="https://code.bdstatic.com/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <script type="text/javascript"
            src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=AyI4O6S4sfPiP2gGaoGBAQq6Uy25Xt8Y"></script>
    <title>地址解析</title>


<body>
<div id="allmap"></div>
<div class='result'>
    点击展示详细的地址：
    <div id='result_l'></div>
    输入详细的地址：
    <input id="input" type="text" />
</div>

</div>

</body>
</html>
<script type="text/javascript">
    // 百度地图API功能
    let map = new BMapGL.Map("allmap");
    let point = new BMapGL.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 12);
    // 创建地址解析器实例
    let myGeo = new BMapGL.Geocoder();
    // 将地址解析结果显示在地图上,并调整地图视野
    let input = $('#input')
    input.blur(()=>{    //输入框失去焦点时
        myGeo.getPoint(input.val(), function(point){
            if (point) {
                alert(point.lng+','+point.lat)
                map.centerAndZoom(point, 16);
                let marker = new BMapGL.Marker(point);
                map.addOverlay(marker);
            }else{
                alert("您选择地址没有解析到结果!");
            }
        }, "武汉市");
    })


    map.addEventListener('click', function (e) {
        //清除地图上所有的覆盖物
        map.clearOverlays();
        //console.log(e);
        let pt = e.latlng;
        let marker = new BMapGL.Marker(new BMapGL.Point(pt.lng, pt.lat));
        map.addOverlay(marker);
        myGeo.getLocation(pt, function (rs) {
            let addComp = rs.addressComponents;
            //$('#result_l').text(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
            //alert(result_l+rs.address)
            $('#result_l').text(rs.address)
        })
    })
</script>
